<?php
/**
* user authenticated sessions storage
* @uses sqlRecordBean
* @uses dateTimeBean
*
* This file belongs to a webappkit package (kit)
* @link http://webappkit.net
* @package databeans
* @subpackage userauthsession
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class DbUserAuthSession extends DataBean {
	/**
	* sets session duration
	* @param int $duration in seconds
	* @access public
	* @return string
	*/
	function setDuration($duration) {
		$this->limited=$this->started;
		return $this->limited->add($duration);
	}/**/

	/**
	* tells wether session is closed ( or limit reached)
	* @access public
	* @return bool
	*/
	function isEnded() {
		if ($this->end->isPast())
			return true;
		return false;
	}/**/

	/**
	* close session
	* @param
	* @access public
	* @return bool
	*/
	function close() {
		$this->closed=datetimebean::now();
        return true;
		//return $this->save();
	}/**/
/*------------------------------------------------------------------------------
                                                                         members
------------------------------------------------------------------------------*/
	/**
	* @var string session key
	*/
	public $key;
	/**
	* creates auto id
	* @access protected
	* @return string
	*/
	function _key_autovalue() {
		return md5(uniqid('',true));
	}/**/
	function _key_check($x) {
		return ereg('^[a-zA-Z0-9]{32}$',$x);
	}

	/**
	* @var string user's ip when opening session
	*/
	public $ip;
	function _ip_autovalue() {
		return $_SERVER['REMOTE_ADDR'];
	}/**/

	/**
	* @var type desc
	* /
	var $uri;
	function _uri_autovalue() {
		return $_SERVER['REQUEST_URI'];
	}/**/

	/**
	* @var int users's id, usually int
	*/
	public $user_id;
	function _user_id_required() {
		return true;
	}

	/**
	* @var DateTimeBean when session started
	*/
	public $start;
	function _start_autovalue() {
		return new datetimebean();
	}/**/
	function & _start_inputfilter($value) {
		if (!is_null($value) and !is_a($value,'datetimebean'))
			$value=&new datetimebean($value);
		return $value;
	}

	/**
	* @var DateTimeBean session's end : either planned or manual close date
	*/
	public $end;
	function _end_autovalue() {
		return new datetimebean();
	}/**/
	function & _end_inputfilter($value) {
		if (!is_null($value) and !is_a($value,'datetimebean'))
			$value=&new datetimebean($value);
		return $value;
	}

}
